#!/bin/sh
# written by Ole Henrik Jahren <olehenja@stud.ntnu.no>
#
# Should probably be rewritten in perl by now, it is starting to grow
# thorns.

for i in application/modules/*; do
	for j in $i/controllers/*.php; do
		module=$(echo $i|sed -n -e 's:.*/\([0-9a-zA-Z]\{1,\}\)$:\1:p'|tr '[A-Z]' '[a-z]')
		controller=$(echo $j|sed -n -e 's:.*/\([0-9a-zA-Z]\{1,\}\)Controller\.php$:\1:p'|tr '[A-Z]' '[a-z]')
		sed -n -e "s/[ \\t]*public[ \\t]\\{1,\\}function[ \\t]\{1,\\}\\([a-zA-Z][0-9a-zA-Z]*\\)Action[ \\t]*(.*/\\1/p" $j|
		while read action; do
			action=$(echo "$action"|sed -e 's/\(.\)\([A-Z]\)/\1-\2/g'|tr '[A-Z]' '[a-z]')
			echo "INSERT INTO redcross_permission (name, description) VALUES('$module:$controller:$action', '$action in $controller');"
		done
	done
done
